"""
优化结果界面
"""
import time
import threading
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import Simulation
from lib import db as database

class Interact_list(QObject):
    # 定义信号,该信号会在js中绑定一个js方法.
    list_send_to_js = pyqtSignal(list)

    def __init__(self, parent=None):
        super().__init__(parent)
        # 交互对象接收到js调用后执行的回调函数.
        self.receive_str_from_js_callback = None


class Form1(QWidget, Simulation.Simulation_cfg):
    def __init__(self):
        super().__init__()

        self.db = database.database()

        self.btnPress1 = QPushButton("路口PI值")
        self.btnPress2 = QPushButton("速度曲线")
        self.btnPress3 = QPushButton("尾气排放")
        self.btnPress1.setFixedSize(170, 60)
        self.btnPress2.setFixedSize(170, 60)
        self.btnPress3.setFixedSize(170, 60)

        qss_style = ''' 
                                     QPushButton{
                                            text-align : center;
                                            font-family:Microsoft Yahei;
                                            font-size:12pt;
                                            background-color : #F5F5F5;
                                            border-color: gray;
                                            border-width: 2px;
                                            border-radius: 10px;
                                            padding: 1px;
                                            height : 14px;
                                            border-style: outset;
                                        }
                                         QPushButton:pressed{
                                            text-align : center;
                                            font-family:Microsoft Yahei;
                                            font-size:12pt;
                                            background-color : #ADD8E6;
                                            border-color: gray;
                                            border-width: 2px;
                                            border-radius: 10px;
                                            padding: 1px;
                                            height : 14px;
                                            border-style: outset;
                                        }
                                     '''
        self.btnPress1.setStyleSheet(qss_style)
        self.btnPress2.setStyleSheet(qss_style)
        self.btnPress3.setStyleSheet(qss_style)

        self.browser = QWebEngineView()
        self.browser.load(QUrl("https://li_hong_chen.gitee.io/bus_project/html/chart_PI.html"))

        # qt与js通信
        self.web_channel = Interact_list()
        channel = QWebChannel(self.browser.page())
        channel.registerObject('Web_Chart', self.web_channel)
        self.browser.page().setWebChannel(channel)

        self.btn_layout = QVBoxLayout()
        self.btn_layout.addWidget(self.btnPress1)
        self.btn_layout.addWidget(self.btnPress2)
        self.btn_layout.addWidget(self.btnPress3)

        self.chart_layout = QHBoxLayout()
        self.chart_layout.addItem(self.btn_layout)
        self.chart_layout.addWidget(self.browser)
        self.chart_layout.setStretchFactor(self.btn_layout, 1)
        self.chart_layout.setStretchFactor(self.browser, 11)
        self.setLayout(self.chart_layout)

        self.btnPress1.clicked.connect(self.btnPress1_Clicked)
        self.btnPress2.clicked.connect(self.btnPress2_Clicked)
        self.btnPress3.clicked.connect(self.btnPress3_Clicked)

        th_pic01 = threading.Thread(target=self.th_updata_speed)
        th_pic01.start()

    def th_updata_speed(self):
        while True:
            self.Updata_Speed()
            time.sleep(1)
            self.Updata_discharge()
            time.sleep(1)

    def Updata_Speed(self):
        """
        更新公交车速度
        以及更新尾气排放
        """
        all_data = self.db.Read_His_Bus()
        bus_speed = []
        for item in all_data:
            bus_speed.append(float(item[3]))
        bus_speed.append(1)
        self.web_channel.list_send_to_js.emit(bus_speed)


    def Updata_discharge(self):
        all_data = self.db.Read_His_Bus()
        discharge = []
        discharge_CO = []
        discharge_CO2 = []
        discharge_HC = []
        discharge_NOX = []
        for item in all_data:
            discharge_CO.append(float(item[7]))
            discharge_CO2.append(float(item[8]))
            discharge_HC.append(float(item[9]))
            discharge_NOX.append(float(item[10]))
        discharge.append(discharge_CO)
        discharge.append(discharge_CO2)
        discharge.append(discharge_HC)
        discharge.append(discharge_NOX)
        discharge.append(3)
        self.web_channel.list_send_to_js.emit(discharge)

    def btnPress1_Clicked(self):
        self.btnPress1.setDown(True)
        self.btnPress2.setDown(False)
        self.btnPress3.setDown(False)

        self.web_channel = Interact_list()
        channel = QWebChannel(self.browser.page())
        channel.registerObject('Web_Chart', self.web_channel)
        self.browser.page().setWebChannel(channel)

        self.browser.load(QUrl("https://li_hong_chen.gitee.io/bus_project/html/chart_PI.html"))

    def btnPress2_Clicked(self):
        self.btnPress1.setDown(False)
        self.btnPress2.setDown(True)
        self.btnPress3.setDown(False)

        self.web_channel = Interact_list()
        channel = QWebChannel(self.browser.page())
        channel.registerObject('Web_Chart', self.web_channel)
        self.browser.page().setWebChannel(channel)

        self.browser.load(QUrl("https://li_hong_chen.gitee.io/bus_project/html/chart_speed.html"))

    def btnPress3_Clicked(self):
        self.btnPress1.setDown(False)
        self.btnPress2.setDown(False)
        self.btnPress3.setDown(True)

        self.web_channel = Interact_list()
        channel = QWebChannel(self.browser.page())
        channel.registerObject('Web_Chart', self.web_channel)
        self.browser.page().setWebChannel(channel)

        self.browser.load(QUrl("https://li_hong_chen.gitee.io/bus_project/html/chart_discharge.html"))
